package cn.xfdzx.android.apps.shop.fragment;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.HomeVPAdapter;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.evenbus.HomeTypeMessage;
import cn.xfdzx.android.apps.shop.fragment.type.MarketClassificationFragment;
import cn.xfdzx.android.apps.shop.fragment.type.TypeCommunityRetailFragment;
import cn.xfdzx.android.apps.shop.fragment.type.TypeMarketPurchaseFragment;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.view.NoScrollViewPager;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

public class TwoFragment extends BaseMvpFragment {

    TabLayout mTabLayout;
    NoScrollViewPager vpFragmentTwo;
    LinearLayout llState;
    private RelativeLayout.LayoutParams item_bar;
    private List<Fragment> mFragments;
    private String[] title = {"社区零售", "新发地市场采购"};
    private HomeVPAdapter homeVPAdapter;

    @Override
    protected void initView(View view) {
        mTabLayout = view.findViewById(R.id.tl_fragment_two_title);
        vpFragmentTwo = view.findViewById(R.id.vp_fragment_two);
        llState = view.findViewById(R.id.ll_state);
        EventBus.getDefault().register(this);

        init();
    }

    private void init() {
//        initHigh();
        mFragments = new ArrayList<>();
        mFragments.add(TypeCommunityRetailFragment.newInstance(1));//社区零售
//        mFragments.add(TypeMarketPurchaseFragment.newInstance(1));//市场采购
        mFragments.add(new MarketClassificationFragment());//市场采购
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.picture_color_transparent)));
        for (String tab : title) {
            mTabLayout.addTab(mTabLayout.newTab().setText(tab));
        }

        homeVPAdapter = new HomeVPAdapter(getChildFragmentManager(), title, mFragments);
        vpFragmentTwo.setAdapter(homeVPAdapter);


        mTabLayout.setupWithViewPager(vpFragmentTwo);

        if (TYPE_IS_RETAIL == 0) {
            vpFragmentTwo.setCurrentItem(0);
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_FB9129), getResources().getColor(R.color.color_FB9129));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_FB9129));
        } else {
            vpFragmentTwo.setCurrentItem(1);
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_02C557), getResources().getColor(R.color.color_02C557));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_02C557));
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_two;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(HomeTypeMessage messageEvent) {
        if (messageEvent.getMessage() == 0) {
            vpFragmentTwo.setCurrentItem(0);
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_FB9129), getResources().getColor(R.color.color_FB9129));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_FB9129));
        } else {
            vpFragmentTwo.setCurrentItem(1);
            mTabLayout.setTabTextColors(getResources().getColor(R.color.color_02C557), getResources().getColor(R.color.color_02C557));
            mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_02C557));
        }
    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(getActivity());
        llState.setLayoutParams(item_bar);
    }

    public static TwoFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        TwoFragment fragment = new TwoFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
